﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Text;

namespace Cn.Cellula.Client {

    public class ProductActor {
        private String _CategoryKey;
        public String CategoryKey {
            get { return _CategoryKey; }
            set { _CategoryKey = value; }
        }

        private String _ProductKey;
        public String ProductKey {
            get { return _ProductKey; }
            set { _ProductKey = value; }
        }

    }
    public class ProductStep{
        private String _Url;
        public String Url {
            get { return _Url; }
            set { _Url = value; }
        }

        private String _Title;
        public String Title {
            get { return _Title; }
            set { _Title = value; }
        }
    }

    public class ProductEditBiz {
        public const String SESS_PRODUCT_FLOW = "cellula_product_flow";
        private IList<ProductStep> steps = new List<ProductStep>();
        public ProductEditBiz() {
            ProductStep ps = new ProductStep();
            ps.Url = "AddCaSelect.aspx";
            ps.Title = "选择分类";
            steps.Add(ps);
            ps = new ProductStep();
            ps.Url = "AddBaseInfo.aspx";
            ps.Title = "基本属性";
            steps.Add(ps);
            ps = new ProductStep();
            ps.Url = "AddAtlInfo.aspx";
            ps.Title = "模板属性";
            steps.Add(ps);
            ps = new ProductStep();
            ps.Url = "AddSaleInfo.aspx";
            ps.Title = "销售属性";
            steps.Add(ps);
            ps = new ProductStep();
            ps.Url = "List.aspx";
            ps.Title = "完成";
            steps.Add(ps);
        }
        
        public Int32 ParseStep(String rUrl) {
            for (int i = 0; i < steps.Count;i++ ) {
                ProductStep ps = steps[i];
                if (rUrl.ToLower().IndexOf(ps.Url.ToLower()) != -1)
                    return i;
            }
            return 0;
        }

        public void GoPrevious(HttpRequest rRequest, HttpResponse rResponse) {
            Int32 step = ParseStep(rRequest.Url.ToString());
            step -= 1;
            if (step >=0)
                rResponse.Redirect(steps[step].Url);
        }

        public void GoNext(HttpRequest rRequest, HttpResponse rResponse) {
            Int32 step = ParseStep(rRequest.Url.ToString());
            step += 1;
            if(step<steps.Count)
                rResponse.Redirect(steps[step].Url);
        }

        public void Finish(HttpResponse rResponse) {
            if(steps.Count>0)
                rResponse.Redirect(steps[steps.Count-1].Url);
        }

        public void PushFlow(HttpSessionState sess,ProductActor pa) {
            sess[ProductEditBiz.SESS_PRODUCT_FLOW] = pa;
        }
        public ProductActor PopFlow(HttpSessionState sess) {
            try {
                return (ProductActor)sess[ProductEditBiz.SESS_PRODUCT_FLOW];
            } catch {
                return null;
            }
        }
        public void ClearFlow(HttpSessionState sess) {
            sess[ProductEditBiz.SESS_PRODUCT_FLOW] = null;
            sess.Remove(ProductEditBiz.SESS_PRODUCT_FLOW);
        }

        public String FlowPath(String rUrl) {
            Int32 step = ParseStep(rUrl);
            StringBuilder sb = new StringBuilder();
            sb.Append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr>");
            sb.Append("<td class=\"product_add_flow_item_f\">增加产品：</td>");
            for (int i = 0; i < steps.Count;i++ ) {
                ProductStep ps = steps[i];
                if (step<i){
                    sb.Append("<td class=\"product_add_flow_item\">");
                } else if (step == i) {
                    sb.Append("<td class=\"product_add_flow_item_a\">");
                } else {
                    sb.Append("<td class=\"product_add_flow_item_o\">");
                }
                sb.Append((i+1)+"、"+ps.Title);
                sb.Append("</td>");
            }
            sb.Append("</tr></table>");
            return sb.ToString();
        }
    }
}
